/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.tabs.results.index;

import java.awt.Frame;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import systole.domain.analysis.results.ParametersKind;
import systole.domain.analysis.results.averages.AverageByDecade;
import systole.domain.persons.Patient;
import systole.domain.report.Report;
import systole.domain.report.template.Comparative;
import systole.exceptions.ExceptionDAO;
import systole.view.tabs.results.IndexesTableModel;
import systole.view.tabs.results.ResultDialogController;

/**
 *
 * @author jmj
 */
public class ControllerIndex extends ResultDialogController {

    private Patient patient;

    public ControllerIndex(Patient patient) {
        super();
        this.patient = patient;
    }

    @Override
    protected void initFormsControls() throws ExceptionDAO {
        JDialogIndex dialogIndex = (JDialogIndex) this.form;
        AverageByDecade averageByDecade = this.facadeDB.getAveragesBroker().getAverageByPatient(patient);
        List<Comparative> comparatives = this.facadeDB.getComparativeBroker().getAllComparatives();

        dialogIndex.getjTableIndexes().setModel(new IndexesTableModel(averageByDecade));
        JComboBox comboBox = new JComboBox();
        for (int i = 0; i < comparatives.size(); i++) {
            comboBox.addItem(comparatives.get(i).getDescription());
        }
        TableColumn column = dialogIndex.getjTableIndexes().getColumnModel().getColumn(1);
        column.setCellEditor(new DefaultCellEditor(comboBox));

        DefaultTableCellRenderer renderer =
                new DefaultTableCellRenderer();
        renderer.setToolTipText("Haga click para seleccinar una opción");

        column.setCellRenderer(renderer);
        dialogIndex.getjTableIndexes().setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    }

    @Override
    protected JDialog createForm(Frame parent) {
        return new JDialogIndex(parent, this);
    }

    @Override
    protected void loadIconOnForm() {

    }

    @Override
    protected void loadReportOnForm(Report report) {
        JDialogIndex dialogIndex = (JDialogIndex) this.form;
        String comp = this.getComparativeFromComment(report.getCommentAOD(), ParametersKind.AOD);
        dialogIndex.getjTableIndexes().getModel().setValueAt(comp, 0, 1);
        comp = this.getComparativeFromComment(report.getCommentIAR(), ParametersKind.IAR);
        dialogIndex.getjTableIndexes().getModel().setValueAt(comp, 1, 1);
        comp = this.getComparativeFromComment(report.getCommentAOS(), ParametersKind.AOS);
        dialogIndex.getjTableIndexes().getModel().setValueAt(comp, 2, 1);

    }

    @Override
    protected void loadReportFromForm() {
        JDialogIndex dialogIndex = (JDialogIndex) this.form;
        String commentIAR = "";
        if (dialogIndex.getjTableIndexes().getModel().getValueAt(1, 1) != null) {
            commentIAR = commentIAR.concat(((dialogIndex.getjTableIndexes().getModel().getValueAt(1, 0) == null) ? "" : (String) dialogIndex.getjTableIndexes().getModel().getValueAt(1, 0) + " "));
            commentIAR = commentIAR.concat(((dialogIndex.getjTableIndexes().getModel().getValueAt(1, 1) == null) ? "" : (String) dialogIndex.getjTableIndexes().getModel().getValueAt(1, 1) + " "));
            commentIAR = commentIAR.concat(((dialogIndex.getjTableIndexes().getModel().getValueAt(1, 2) == null) ? "" : (String) dialogIndex.getjTableIndexes().getModel().getValueAt(1, 2) + "."));
        }
        String commentAOD = "";
        if (dialogIndex.getjTableIndexes().getModel().getValueAt(0, 1) != null) {
            commentAOD = commentAOD.concat(((dialogIndex.getjTableIndexes().getModel().getValueAt(0, 0) == null) ? "" : (String) dialogIndex.getjTableIndexes().getModel().getValueAt(0, 0) + " "));
            commentAOD = commentAOD.concat(((dialogIndex.getjTableIndexes().getModel().getValueAt(0, 1) == null) ? "" : (String) dialogIndex.getjTableIndexes().getModel().getValueAt(0, 1) + " "));
            commentAOD = commentAOD.concat(((dialogIndex.getjTableIndexes().getModel().getValueAt(0, 2) == null) ? "" : (String) dialogIndex.getjTableIndexes().getModel().getValueAt(0, 2) + "."));
        }
        String commentAOS = "";
        if (dialogIndex.getjTableIndexes().getModel().getValueAt(2, 1) != null) {
            commentAOS = commentAOS.concat(((dialogIndex.getjTableIndexes().getModel().getValueAt(2, 0) == null) ? "" : (String) dialogIndex.getjTableIndexes().getModel().getValueAt(2, 0) + " "));
            commentAOS = commentAOS.concat(((dialogIndex.getjTableIndexes().getModel().getValueAt(2, 1) == null) ? "" : (String) dialogIndex.getjTableIndexes().getModel().getValueAt(2, 1) + " "));
            commentAOS = commentAOS.concat(((dialogIndex.getjTableIndexes().getModel().getValueAt(2, 2) == null) ? "" : (String) dialogIndex.getjTableIndexes().getModel().getValueAt(2, 2) + "."));
        }
        this.reportModel.updateParamsComments(commentAOD, commentAOS, commentIAR);
    }

    private String getComparativeFromComment(String comment, String paramName) {
        if ((comment == null) || (paramName == null)) {
            return null;
        }
        String result = comment.replaceAll(paramName, "");
        int index = result.indexOf("promedio");
        if (index > -1) {
            result = result.substring(0, index).trim();
        }
        return result;
    }
}
